package cn.efunbox.afw.data.redis.serialize;

import org.springframework.core.serializer.support.SerializingConverter;

import java.util.Arrays;

/**
 * Created by Dreampie on 16/9/1.
 */
public class SerializingPrefixConverter extends SerializingConverter {

  private byte[] prefix;

  public SerializingPrefixConverter(byte[] prefix) {
    this.prefix = prefix;
  }

  public byte[] convert(Object key) {
    byte[] rawKey = super.convert(key);
    byte[] prefixedKey = Arrays.copyOf(prefix, prefix.length + rawKey.length);
    System.arraycopy(rawKey, 0, prefixedKey, prefix.length, rawKey.length);
    return prefixedKey;
  }
}
